<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>PrimMST.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="PrimMST code in Java">
<meta NAME="TITLE" CONTENT="PrimMST code in Java">
<meta NAME="KEYWORDS" CONTENT="PrimMST,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>PrimMST.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "PrimMST.java">PrimMST.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac PrimMST.java</span>
<span class="comment"> *  Execution:    java PrimMST filename.txt</span>
<span class="comment"> *  Dependencies: EdgeWeightedGraph.java Edge.java Queue.java</span>
<span class="comment"> *                IndexMinPQ.java UF.java In.java StdOut.java</span>
<span class="comment"> *  Data files:   </span><span class="url">http://algs4.cs.princeton.edu/43mst/tinyEWG.txt</span>
<span class="comment"> *                </span><span class="url">http://algs4.cs.princeton.edu/43mst/mediumEWG.txt</span>
<span class="comment"> *                </span><span class="url">http://algs4.cs.princeton.edu/43mst/largeEWG.txt</span>
<span class="comment"> *</span>
<span class="comment"> *  Compute a minimum spanning forest using Prim's algorithm.</span>
<span class="comment"> *</span>
<span class="comment"> *  %  java PrimMST tinyEWG.txt </span>
<span class="comment"> *  1-7 0.19000</span>
<span class="comment"> *  0-2 0.26000</span>
<span class="comment"> *  2-3 0.17000</span>
<span class="comment"> *  4-5 0.35000</span>
<span class="comment"> *  5-7 0.28000</span>
<span class="comment"> *  6-2 0.40000</span>
<span class="comment"> *  0-7 0.16000</span>
<span class="comment"> *  1.81000</span>
<span class="comment"> *</span>
<span class="comment"> *  % java PrimMST mediumEWG.txt</span>
<span class="comment"> *  1-72   0.06506</span>
<span class="comment"> *  2-86   0.05980</span>
<span class="comment"> *  3-67   0.09725</span>
<span class="comment"> *  4-55   0.06425</span>
<span class="comment"> *  5-102  0.03834</span>
<span class="comment"> *  6-129  0.05363</span>
<span class="comment"> *  7-157  0.00516</span>
<span class="comment"> *  ...</span>
<span class="comment"> *  10.46351</span>
<span class="comment"> *</span>
<span class="comment"> *  % java PrimMST largeEWG.txt</span>
<span class="comment"> *  ...</span>
<span class="comment"> *  647.66307</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">PrimMST</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> class represents a data type for computing a</span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">minimum spanning tree</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> in an edge-weighted graph.</span>
<span class="comment"> *  The edge weights can be positive, zero, or negative and need not</span>
<span class="comment"> *  be distinct. If the graph is not connected, it computes a </span><span class="keyword">&lt;em&gt;</span><span class="comment">minimum</span>
<span class="comment"> *  spanning forest</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, which is the union of minimum spanning trees</span>
<span class="comment"> *  in each connected component. The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">weight()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> method returns the </span>
<span class="comment"> *  weight of a minimum spanning tree and the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">edges()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> method</span>
<span class="comment"> *  returns its edges.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  This implementation uses </span><span class="keyword">&lt;em&gt;</span><span class="comment">Prim's algorithm</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> with an indexed</span>
<span class="comment"> *  binary heap.</span>
<span class="comment"> *  The constructor takes time proportional to </span><span class="keyword">&lt;em&gt;</span><span class="comment">E</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> log </span><span class="keyword">&lt;em&gt;</span><span class="comment">V</span><span class="keyword">&lt;/em&gt;</span>
<span class="comment"> *  and extra space (not including the graph) proportional to </span><span class="keyword">&lt;em&gt;</span><span class="comment">V</span><span class="keyword">&lt;/em&gt;</span><span class="comment">,</span>
<span class="comment"> *  where </span><span class="keyword">&lt;em&gt;</span><span class="comment">V</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is the number of vertices and </span><span class="keyword">&lt;em&gt;</span><span class="comment">E</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is the number of edges.</span>
<span class="comment"> *  Afterwards, the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">weight()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> method takes constant time</span>
<span class="comment"> *  and the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">edges()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> method takes time proportional to </span><span class="keyword">&lt;em&gt;</span><span class="comment">V</span><span class="keyword">&lt;/em&gt;</span><span class="comment">.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation,</span>
<span class="comment"> *  see </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://algs4.cs.princeton.edu/43mst"</span><span class="keyword">&gt;</span><span class="comment">Section 4.3</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of</span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Algorithms, 4th Edition</span><span class="keyword">&lt;/i&gt;</span><span class="comment"> by Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *  For alternate implementations, see {</span><span class="type">@link</span><span class="comment"> LazyPrimMST}, {</span><span class="type">@link</span><span class="comment"> KruskalMST},</span>
<span class="comment"> *  and {</span><span class="type">@link</span><span class="comment"> BoruvkaMST}.</span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">PrimMST</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">double</span><span class="normal"> FLOATING_POINT_EPSILON </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1E-12</span><span class="symbol">;</span>

<span class="normal">    </span><span class="keyword">private</span><span class="normal"> Edge</span><span class="symbol">[]</span><span class="normal"> edgeTo</span><span class="symbol">;</span><span class="normal">        </span><span class="comment">// edgeTo[v] = shortest edge from tree vertex to non-tree vertex</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> distTo</span><span class="symbol">;</span><span class="normal">      </span><span class="comment">// distTo[v] = weight of shortest such edge</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="symbol">[]</span><span class="normal"> marked</span><span class="symbol">;</span><span class="normal">     </span><span class="comment">// marked[v] = true if v on tree, false otherwise</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">IndexMinPQ&lt;Double&gt;</span><span class="normal"> pq</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Compute a minimum spanning tree (or forest) of an edge-weighted graph.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> G the edge-weighted graph</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">PrimMST</span><span class="symbol">(</span><span class="usertype">EdgeWeightedGraph</span><span class="normal"> G</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        edgeTo </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Edge</span><span class="symbol">[</span><span class="normal">G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">()];</span>
<span class="normal">        distTo </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">double</span><span class="symbol">[</span><span class="normal">G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">()];</span>
<span class="normal">        marked </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">boolean</span><span class="symbol">[</span><span class="normal">G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">()];</span>
<span class="normal">        pq </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> IndexMinPQ</span><span class="symbol">&lt;</span><span class="normal">Double</span><span class="symbol">&gt;(</span><span class="normal">G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">());</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> v</span><span class="symbol">++)</span>
<span class="normal">            distTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> Double</span><span class="symbol">.</span><span class="normal">POSITIVE_INFINITY</span><span class="symbol">;</span>

<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> v</span><span class="symbol">++)</span><span class="normal">      </span><span class="comment">// run from each vertex to find</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">marked</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">])</span><span class="normal"> </span><span class="function">prim</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">,</span><span class="normal"> v</span><span class="symbol">);</span><span class="normal">      </span><span class="comment">// minimum spanning forest</span>

<span class="normal">        </span><span class="comment">// check optimality conditions</span>
<span class="normal">        </span><span class="keyword">assert</span><span class="normal"> </span><span class="function">check</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// run Prim's algorithm in graph G, starting from vertex s</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">prim</span><span class="symbol">(</span><span class="usertype">EdgeWeightedGraph</span><span class="normal"> G</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> s</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        distTo</span><span class="symbol">[</span><span class="normal">s</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">;</span>
<span class="normal">        pq</span><span class="symbol">.</span><span class="function">insert</span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">,</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">s</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">pq</span><span class="symbol">.</span><span class="function">isEmpty</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> pq</span><span class="symbol">.</span><span class="function">delMin</span><span class="symbol">();</span>
<span class="normal">            </span><span class="function">scan</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">,</span><span class="normal"> v</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// scan vertex v</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">scan</span><span class="symbol">(</span><span class="usertype">EdgeWeightedGraph</span><span class="normal"> G</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> v</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        marked</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Edge</span><span class="normal"> e </span><span class="symbol">:</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">adj</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> w </span><span class="symbol">=</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">other</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">marked</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">])</span><span class="normal"> </span><span class="keyword">continue</span><span class="symbol">;</span><span class="normal">         </span><span class="comment">// v-w is obsolete edge</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">weight</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">&lt;</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">])</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                distTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">weight</span><span class="symbol">();</span>
<span class="normal">                edgeTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> e</span><span class="symbol">;</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">pq</span><span class="symbol">.</span><span class="function">contains</span><span class="symbol">(</span><span class="normal">w</span><span class="symbol">))</span><span class="normal"> pq</span><span class="symbol">.</span><span class="function">decreaseKey</span><span class="symbol">(</span><span class="normal">w</span><span class="symbol">,</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]);</span>
<span class="normal">                </span><span class="keyword">else</span><span class="normal">                pq</span><span class="symbol">.</span><span class="function">insert</span><span class="symbol">(</span><span class="normal">w</span><span class="symbol">,</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the edges in a minimum spanning tree (or forest).</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the edges in a minimum spanning tree (or forest) as</span>
<span class="comment">     *    an iterable of edges</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">Iterable&lt;Edge&gt;</span><span class="normal"> </span><span class="function">edges</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">Queue&lt;Edge&gt;</span><span class="normal"> mst </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Queue</span><span class="symbol">&lt;</span><span class="normal">Edge</span><span class="symbol">&gt;();</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> edgeTo</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span><span class="normal"> v</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="usertype">Edge</span><span class="normal"> e </span><span class="symbol">=</span><span class="normal"> edgeTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">];</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">e </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                mst</span><span class="symbol">.</span><span class="function">enqueue</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> mst</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the sum of the edge weights in a minimum spanning tree (or forest).</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the sum of the edge weights in a minimum spanning tree (or forest)</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">weight</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> weight </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Edge</span><span class="normal"> e </span><span class="symbol">:</span><span class="normal"> </span><span class="function">edges</span><span class="symbol">())</span>
<span class="normal">            weight </span><span class="symbol">+=</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">weight</span><span class="symbol">();</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> weight</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">// check optimality conditions (takes time proportional to E V lg* V)</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">check</span><span class="symbol">(</span><span class="usertype">EdgeWeightedGraph</span><span class="normal"> G</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">        </span><span class="comment">// check weight</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> totalWeight </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Edge</span><span class="normal"> e </span><span class="symbol">:</span><span class="normal"> </span><span class="function">edges</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            totalWeight </span><span class="symbol">+=</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">weight</span><span class="symbol">();</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">Math</span><span class="symbol">.</span><span class="function">abs</span><span class="symbol">(</span><span class="normal">totalWeight </span><span class="symbol">-</span><span class="normal"> </span><span class="function">weight</span><span class="symbol">())</span><span class="normal"> </span><span class="symbol">&gt;</span><span class="normal"> FLOATING_POINT_EPSILON</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            System</span><span class="symbol">.</span><span class="normal">err</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"Weight of edges does not equal weight(): %f vs. %f</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">,</span><span class="normal"> totalWeight</span><span class="symbol">,</span><span class="normal"> </span><span class="function">weight</span><span class="symbol">());</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// check that it is acyclic</span>
<span class="normal">        </span><span class="usertype">UF</span><span class="normal"> uf </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">UF</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">());</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Edge</span><span class="normal"> e </span><span class="symbol">:</span><span class="normal"> </span><span class="function">edges</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">either</span><span class="symbol">(),</span><span class="normal"> w </span><span class="symbol">=</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">other</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">uf</span><span class="symbol">.</span><span class="function">connected</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">,</span><span class="normal"> w</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                System</span><span class="symbol">.</span><span class="normal">err</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"Not a forest"</span><span class="symbol">);</span>
<span class="normal">                </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            uf</span><span class="symbol">.</span><span class="function">union</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">,</span><span class="normal"> w</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// check that it is a spanning forest</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Edge</span><span class="normal"> e </span><span class="symbol">:</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">edges</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">either</span><span class="symbol">(),</span><span class="normal"> w </span><span class="symbol">=</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">other</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">uf</span><span class="symbol">.</span><span class="function">connected</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">,</span><span class="normal"> w</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                System</span><span class="symbol">.</span><span class="normal">err</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"Not a spanning forest"</span><span class="symbol">);</span>
<span class="normal">                </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// check that it is a minimal spanning forest (cut optimality conditions)</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Edge</span><span class="normal"> e </span><span class="symbol">:</span><span class="normal"> </span><span class="function">edges</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">            </span><span class="comment">// all edges in MST except e</span>
<span class="normal">            uf </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">UF</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">());</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Edge</span><span class="normal"> f </span><span class="symbol">:</span><span class="normal"> </span><span class="function">edges</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="type">int</span><span class="normal"> x </span><span class="symbol">=</span><span class="normal"> f</span><span class="symbol">.</span><span class="function">either</span><span class="symbol">(),</span><span class="normal"> y </span><span class="symbol">=</span><span class="normal"> f</span><span class="symbol">.</span><span class="function">other</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">f </span><span class="symbol">!=</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> uf</span><span class="symbol">.</span><span class="function">union</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>

<span class="normal">            </span><span class="comment">// check that e is min weight edge in crossing cut</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Edge</span><span class="normal"> f </span><span class="symbol">:</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">edges</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="type">int</span><span class="normal"> x </span><span class="symbol">=</span><span class="normal"> f</span><span class="symbol">.</span><span class="function">either</span><span class="symbol">(),</span><span class="normal"> y </span><span class="symbol">=</span><span class="normal"> f</span><span class="symbol">.</span><span class="function">other</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">uf</span><span class="symbol">.</span><span class="function">connected</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">f</span><span class="symbol">.</span><span class="function">weight</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">&lt;</span><span class="normal"> e</span><span class="symbol">.</span><span class="function">weight</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                        System</span><span class="symbol">.</span><span class="normal">err</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"Edge "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> f </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" violates cut optimality conditions"</span><span class="symbol">);</span>
<span class="normal">                        </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">                    </span><span class="cbracket">}</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Unit tests the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">PrimMST</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> data type.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">In</span><span class="normal"> in </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">In</span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="usertype">EdgeWeightedGraph</span><span class="normal"> G </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">EdgeWeightedGraph</span><span class="symbol">(</span><span class="normal">in</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">PrimMST</span><span class="normal"> mst </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">PrimMST</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Edge</span><span class="normal"> e </span><span class="symbol">:</span><span class="normal"> mst</span><span class="symbol">.</span><span class="function">edges</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"%.5f</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">,</span><span class="normal"> mst</span><span class="symbol">.</span><span class="function">weight</span><span class="symbol">());</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
